home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Mac / GUSI-mods / GUSI_P.h next >
Encoding:
C/C++ Source or Header  |  1996-10-17  |  11.4 KB  |  453 lines  |  [TEXT/CWIE]

  1. /*********************************************************************
  2. Project    :    GUSI                -    Grand Unified Socket Interface
  3. File        :    GUSI_P.h            -    Private stuff
  4. Author    :    Matthias Neeracher
  5. Language    :    MPW C/C++
  6.  
  7. $Log: GUSI_P.h,v $
  8. Revision 1.3  1994/12/31  01:30:26  neeri
  9. Reorganize filename dispatching.
  10.  
  11. Revision 1.2  1994/08/10  00:41:05  neeri
  12. Sanitized for universal headers.
  13.  
  14. Revision 1.1  1994/02/25  02:57:01  neeri
  15. Initial revision
  16.  
  17. Revision 0.22  1993/07/17  00:00:00  neeri
  18. GUSIRingBuffer::proc -> defproc
  19.  
  20. Revision 0.21  1993/07/17  00:00:00  neeri
  21. GUSIO_MAX_DOMAIN -> AF_MAX
  22.  
  23. Revision 0.20  1993/06/27  00:00:00  neeri
  24. Socket::{pre,post}_select
  25.  
  26. Revision 0.19  1993/06/27  00:00:00  neeri
  27. Socket::ftruncate
  28.  
  29. Revision 0.18  1993/02/09  00:00:00  neeri
  30. Socket::lurking, Socket::lurkdescr
  31.  
  32. Revision 0.17  1993/01/31  00:00:00  neeri
  33. GUSIConfiguration::daemon
  34.  
  35. Revision 0.16  1993/01/17  00:00:00  neeri
  36. Destructors for Socketdomain
  37.  
  38. Revision 0.15  1993/01/17  00:00:00  neeri
  39. SAFESPIN
  40.  
  41. Revision 0.14  1993/01/03  00:00:00  neeri
  42. GUSIConfig
  43.  
  44. Revision 0.13  1992/09/24  00:00:00  neeri
  45. Include GUSIRsrc_P.h
  46.  
  47. Revision 0.12  1992/09/13  00:00:00  neeri
  48. SPINVOID didn't return
  49.  
  50. Revision 0.11  1992/08/30  00:00:00  neeri
  51. AppleTalkIdentity()
  52.  
  53. Revision 0.10  1992/08/03  00:00:00  neeri
  54. RingBuffer
  55.  
  56. Revision 0.9  1992/07/30  00:00:00  neeri
  57. Initializer Features
  58.  
  59. Revision 0.8  1992/07/26  00:00:00  neeri
  60. UnixSockets.choose()
  61.  
  62. Revision 0.7  1992/07/13  00:00:00  neeri
  63. Make AppleTalkSockets global
  64.  
  65. Revision 0.6  1992/06/27  00:00:00  neeri
  66. choose(), hasNewSF
  67.  
  68. Revision 0.5  1992/06/07  00:00:00  neeri
  69. Feature
  70.  
  71. Revision 0.4  1992/05/21  00:00:00  neeri
  72. Implemented select()
  73.  
  74. Revision 0.3  1992/04/19  00:00:00  neeri
  75. C++ rewrite
  76.  
  77. Revision 0.2  1992/04/18  00:00:00  neeri
  78. changed read/write/send/recv dispatchers
  79.  
  80. Revision 0.1  1992/04/18  00:00:00  neeri
  81. ppc Domain
  82.  
  83. *********************************************************************/
  84.  
  85. #ifndef __GUSI_P__
  86. #define __GUSI_P__
  87.  
  88. #define __useAppleExts__
  89.  
  90. #include <GUSI.h>
  91. #include <GUSIRsrc_P.h>
  92. #include <TFileSpec.h>
  93.  
  94.  
  95. #include <sys/errno.h>
  96. #include <sys/uio.h>
  97. #include <sys/socket.h>
  98.  
  99. extern "C" {
  100.  
  101. #include <stdio.h>
  102. #include <string.h>
  103.  
  104. int         GUSI_error(int err);
  105. void *    GUSI_error_nil(int err);
  106. }
  107.  
  108. #include <Memory.h>
  109. #include <GestaltEqu.h>
  110. #include <Traps.h>
  111. #include <AppleEvents.h>
  112.  
  113. #ifndef GUSI_NO_UNIVERSAL
  114. #include <MixedMode.h>
  115. #endif
  116.  
  117. #if GENERATING68K
  118. #pragma segment GUSI
  119. #endif
  120.  
  121. #define GUSI_MAX_DOMAIN            AF_MAX
  122. #define DEFAULT_BUFFER_SIZE    4096
  123.  
  124. /*
  125.  *    In use and shutdown status.
  126.  */
  127. #define    SOCK_STATUS_USED        0x1        /* Used socket table entry */
  128. #define    SOCK_STATUS_NOREAD    0x2        /* No more reading allowed from socket */
  129. #define    SOCK_STATUS_NOWRITE    0x4        /* No more writing allowed to socket */
  130.  
  131. /*
  132.  *    Socket connection states.
  133.  */
  134. #define    SOCK_STATE_NO_STREAM        0    /* Socket doesn't have a MacTCP stream yet */
  135. #define    SOCK_STATE_UNCONNECTED    1    /* Socket is unconnected. */
  136. #define    SOCK_STATE_LISTENING        2    /* Socket is listening for connection. */
  137. #define    SOCK_STATE_LIS_CON        3    /* Socket is in transition from listen to connected. */
  138. #define    SOCK_STATE_CONNECTING    4    /* Socket is initiating a connection. */
  139. #define    SOCK_STATE_CONNECTED        5    /* Socket is connected. */
  140. #define    SOCK_STATE_CLOSING      6    /* Socket is closing */
  141. #define    SOCK_STATE_LIS_CLOSE    7    /* Socket closed while listening */
  142.  
  143. #define        min(a,b)                ( (a) < (b) ? (a) : (b))
  144. #define        max(a,b)                ( (a) > (b) ? (a) : (b))
  145.  
  146. extern GUSISpinFn GUSISpin;
  147. extern "C" int GUSIDefaultSpin(spin_msg, long);
  148. extern int GUSICheckAlarm();
  149.  
  150. #define GUSI_INTERRUPT(mesg,param)    (GUSICheckAlarm() || (GUSISpin && (*GUSISpin)(mesg,param)))
  151.  
  152. /* SPIN returns a -1 on user cancel for fn returning integers */
  153. #define        SPIN(cond,mesg,param)                            \
  154.                     do {                                                \
  155.                         if (GUSI_INTERRUPT(mesg,param))        \
  156.                             return GUSI_error(EINTR);            \
  157.                     } while(cond)
  158.  
  159. /* SPINP returns a NULL on user cancel, for fn returning pointers */                
  160. #define        SPINP(cond,mesg,param)                            \
  161.                     do {                                                \
  162.                         if (GUSI_INTERRUPT(mesg,param)) {    \
  163.                             GUSI_error(EINTR);                    \
  164.                             return NULL;                            \
  165.                         }                                                \
  166.                     } while(cond)
  167.  
  168. /* SPINVOID just returns on user cancel, for fn returning void */                
  169. #define        SPINVOID(cond,mesg,param)                        \
  170.                     do {                                                \
  171.                         if (GUSI_INTERRUPT(mesg,param)) {    \
  172.                                 GUSI_error(EINTR);                \
  173.                                 return;                                \
  174.                             }                                            \
  175.                     } while(cond)
  176.                     
  177. /* SAFESPIN doesn't return, you have to check errno */                
  178. #define        SAFESPIN(cond,mesg,param)                        \
  179.                     do {                                                \
  180.                         if (GUSI_INTERRUPT(mesg,param)) {    \
  181.                             GUSI_error(EINTR);                    \
  182.                             break;                                    \
  183.                         } else                                        \
  184.                             errno = 0;                                \
  185.                     } while(cond)
  186.                     
  187. class SocketTable;
  188.  
  189. #if PRAGMA_ALIGN_SUPPORTED
  190. #pragma options align=mac68k
  191. #endif
  192.  
  193. class Socket {
  194.     friend class SocketTable;
  195.     
  196.     short            refCount;
  197. protected:
  198.                     Socket();
  199. public:
  200.     virtual int    bind(void * name, int namelen);
  201.     virtual int connect(void * address, int addrlen);
  202.     virtual int listen(int qlen);
  203.     virtual Socket * accept(void * address, int * addrlen);
  204.     virtual int    read(void * buffer, int buflen);
  205.     virtual int write(void * buffer, int buflen);
  206.     virtual int recvfrom(void * buffer, int buflen, int flags, void * from, int * fromlen);
  207.     virtual int sendto(void * buffer, int buflen, int flags, void * to, int tolen);
  208.     virtual int getsockname(void * name, int * namelen);
  209.     virtual int getpeername(void * name, int * namelen);
  210.     virtual int getsockopt(int level, int optname, void *optval, int * optlen);
  211.     virtual int setsockopt(int level, int optname, void *optval, int optlen);
  212.     virtual int    fcntl(unsigned int cmd, int arg);
  213.     virtual int    ioctl(unsigned int request, void *argp);
  214.     virtual int    fstat(struct stat * buf);
  215.     virtual long lseek(long offset, int whence);
  216.     virtual int ftruncate(long offset);
  217.     virtual int    isatty();
  218.     virtual int shutdown(int how);
  219.     virtual void pre_select(Boolean wantRead, Boolean wantWrite, Boolean wantExcept);
  220.     virtual int select(Boolean * canRead, Boolean * canWrite, Boolean * exception);
  221.     virtual void post_select(Boolean wantRead, Boolean wantWrite, Boolean wantExcept);
  222.     virtual         ~Socket();
  223.     
  224.     void operator++()    {    ++refCount;                            }
  225.     void operator--()    {    if (!--refCount) delete this;    }
  226. };
  227.  
  228.  
  229. #if PRAGMA_ALIGN_SUPPORTED
  230. #pragma options align=reset
  231. #endif
  232.  
  233. class SocketDomain {
  234.     static SocketDomain *        domains[GUSI_MAX_DOMAIN];
  235.     static ProcessSerialNumber    process;
  236. protected:
  237.     SocketDomain(int domain);
  238.     virtual ~SocketDomain();
  239. public:
  240.     inline static SocketDomain *    Domain(int domain);
  241.     static void Ready();
  242.     
  243.     // Optionally override the following
  244.     
  245.     virtual Socket * socket(int type, short protocol);
  246.     
  247.     // Optionally override the following
  248.     
  249.     virtual int socketpair(int type, short protocol, Socket * sockets[]);
  250.     
  251.     // Optionally define the following
  252.     
  253.     virtual int choose(
  254.                         int         type, 
  255.                         char *     prompt, 
  256.                         void *     constraint,        
  257.                         int         flags,
  258.                          void *     name, 
  259.                         int *     namelen);
  260.     
  261.     // Never override the following
  262.     
  263.     void DontStrip();
  264. };
  265.  
  266. class SocketTable {
  267.     Socket *    sockets[GUSI_MAX_FD];
  268.     Boolean    needsConsole;
  269. public:
  270.     SocketTable();
  271.     ~SocketTable();
  272.     
  273.     void        InitConsole();
  274.     int        Install(Socket * sock, int start = 0);
  275.     int        Remove(int fd);
  276.     Socket * operator[](int fd);
  277. };
  278.  
  279. struct GUSISuffix {
  280.     char         suffix[4];
  281.     OSType    suffType;
  282.     OSType    suffCreator;
  283. };
  284.  
  285. #if PRAGMA_ALIGN_SUPPORTED
  286. #pragma options align=mac68k
  287. #endif
  288.  
  289. //
  290. // I learned the hard way not to rely on bit field alignments
  291. //
  292.  
  293. struct GUSIConfigRsrc {
  294.     OSType            defaultType;
  295.     OSType            defaultCreator;
  296.     
  297.     char                autoSpin;
  298.     unsigned char    flags;
  299.     
  300.     OSType            version;
  301.     short                numSuffices;
  302.     GUSISuffix         suffices[1];
  303. };
  304.  
  305. #if PRAGMA_ALIGN_SUPPORTED
  306. #pragma options align=reset
  307. #endif
  308.  
  309. struct GUSIConfiguration {
  310.     OSType            defaultType;
  311.     OSType            defaultCreator;
  312.     
  313.     char                autoSpin;
  314.     
  315.     Boolean             noChdir;        // Set current directory without chdir()
  316.     Boolean             accurStat;    // Return # of subdirectories + 2 in st_nlink
  317.     Boolean             hasConsole;    // Do we have our own console ?
  318.     Boolean            noAutoInitGraf;    // Never automatically do InitGraf
  319.     Boolean            sharedOpen;    // Open files with shared permissions
  320.     Boolean            sigPipe;        // raise SIGPIPE on write to closed socket
  321.     Boolean            delayConsole;    // Do not open console until needed
  322.     
  323.     OSType            version;
  324.     short                numSuffices;
  325.     GUSISuffix *    suffices;
  326.     
  327.     GUSIConfiguration();
  328.     void GUSILoadConfiguration(Handle config);
  329.     
  330.     void SetDefaultFType(const TFileSpec & name) const;
  331.     void DoAutoSpin() const;
  332.     void AutoInitGraf()    const {    if (!noAutoInitGraf) DoAutoInitGraf();    }
  333.     void DoAutoInitGraf() const;
  334.     Boolean DelayConsole() const;
  335. private:
  336.     static Boolean firstTime;
  337.     static short    we;
  338. };
  339.  
  340. extern GUSIConfiguration    GUSIConfig;
  341. extern SocketTable                    Sockets;
  342.  
  343. typedef pascal OSErr (*OSErrInitializer)();
  344. typedef pascal void  (*voidInitializer)();
  345.  
  346. class Feature {
  347.     Boolean    good;
  348. public:
  349.     Feature(unsigned short trapNum, TrapType tTyp);
  350.     Feature(OSType type, long value);
  351.     Feature(OSType type, long mask, long value);
  352.     Feature(const Feature & precondition, OSErrInitializer init);
  353.     Feature(OSErrInitializer init);
  354.     Feature(const Feature & precondition, voidInitializer init);
  355.     Feature(voidInitializer init);
  356.     Feature(const Feature & cond1, const Feature & cond2);
  357.  
  358.     operator void*() const {    return (void *) good;    }
  359. };
  360.  
  361. extern Feature hasMakeFSSpec;
  362. extern Feature hasAlias;
  363. extern Feature hasNewSF;
  364. extern Feature hasProcessMgr;
  365. extern Feature hasCRM;
  366. extern Feature hasCTB;
  367. extern Feature hasStdNBP;
  368. extern Feature hasCM;
  369. extern Feature hasFT;
  370. extern Feature hasTM;
  371. extern Feature    hasPPC;
  372. extern Feature hasRevisedTimeMgr;
  373.  
  374. class ScattGath    {
  375.     Handle            scratch;
  376. protected:
  377.     void *            buf;
  378.     int                        len;
  379.     int                        count;
  380.     const struct iovec *    io;
  381.  
  382.     ScattGath(const struct iovec *iov, int cnt);
  383.     virtual ~ScattGath();
  384. public:
  385.     void *            buffer()            {    return buf;            }
  386.     int                buflen()            {    return len;            }
  387.     int                length(int l)    {    return len = l;    }
  388.     operator void *()                    {    return buf;            }
  389. };
  390.  
  391. class Scatterer : public ScattGath {
  392. public:
  393.     Scatterer(const struct iovec *iov, int count);
  394.     virtual ~Scatterer();
  395. };
  396.  
  397. class Gatherer : public ScattGath {
  398. public:
  399.     Gatherer(const struct iovec *iov, int count);
  400.     virtual ~Gatherer();
  401. };
  402.  
  403. typedef pascal void (*Deferred)(void *);
  404.  
  405. class RingBuffer {
  406.     // Valid bytes are between consume and produce
  407.     // Free bytes are between produce and consume
  408.     // bytes between endbuf-spare and endbuf are neither
  409.     Ptr        buffer;
  410.     Ptr        endbuf;
  411.     Ptr         consume;
  412.     Ptr        produce;
  413.     u_short    free;
  414.     u_short    valid;
  415.     u_short    spare;
  416.     Boolean    lock;
  417.     Deferred    defproc;
  418.     void *    arg;
  419.     
  420. public:
  421.                 RingBuffer(u_short bufsiz);
  422.                 ~RingBuffer();
  423.     
  424.     Ptr        Producer(long & len);            //    Find continuous memory for producer
  425.     Ptr        Consumer(long & len);            //    Find continuous memory for consumer
  426.     void        Validate(long len);                // Validate this, unallocate rest
  427.     void         Invalidate(long len);
  428.     void        Produce(Ptr from, long & len);//    Allocate, copy & validate
  429.     void        Consume(Ptr to, long & len);    // Copy & invalidate
  430.     
  431.     long        Free()                                { return free;                                    }        
  432.     long        Valid()                                { return valid;                                }
  433.     
  434.     void         Defer()                                { lock = true;                                    }
  435.     void         Undefer()                            { lock = false; if (defproc) defproc(arg);}
  436.     Boolean    Locked()                                { return lock;                                    }
  437.     void        Later(Deferred def, void * ar){ defproc = def; arg = ar;                    }
  438.     
  439.     operator void *()                                { return buffer;                                }
  440. };
  441.  
  442. Boolean GUSIInterrupt();
  443.  
  444. Boolean CopyIconFamily(short srcResFile, short srcID, short dstResFile, short dstID);
  445.  
  446. pascal OSErr PPCInit_P();
  447.  
  448. OSErr AppleTalkIdentity(short & net, short & node);
  449.  
  450. void CopyC2PStr(const char * cstr, StringPtr pstr);
  451.  
  452. #endif
  453.